import java.util.Arrays;

/**
 * @description:
 * @author: asd
 * @time: 2022/10/3 14:07
 */
public class sds {
    private final static  int SDS_MAX_PREALLOC = 1024*1024;
    //其已占用长度
    private  int len;
    //剩余可用长度
    private int free;
    //保存字符串数据的地方
    private char[] buf;

    public sds() {
        buf = new char[16];
        free = 16;
    }
    public sds(String s){
        buf = new char[s.length() * 2 + 1];
        len = 0;
        sdsstrcat(s.toCharArray(),s.length());
    }
    public sds(int len) {
        this.len = len;
        this.buf = new char[len];
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public int getFree() {
        return free;
    }

    public void setFree(int free) {
        this.free = free;
    }

    public char[] getBuf() {
        return buf;
    }

    public void setBuf(char[] buf) {
        this.buf = buf;
    }
    /***
     * @description: 优化追加操作
     * @param s
     * @return: sds
     * @author: asd
     * @time: 2022/10/3 14:30
     */
    private void sdsMakeRoomFor(sds s,int require_len){
        if(s.free >= require_len) return ;
        int newlen = s.len + require_len;
        if(newlen < SDS_MAX_PREALLOC)
            newlen = 2 * newlen + 1;
        else newlen += SDS_MAX_PREALLOC + 1;
        char[] ss = new char[newlen];
        for (int i = 0; i < s.getLen(); i++) {
            ss[i] = s.buf[i];
        }
        buf =ss;
    }
    private void sdsstrcat(char[] asd,int len1){
        sdsMakeRoomFor(this,len1);
        for (int i = 0; i < len1; i++) {
            buf[len+i] = asd[i];
        }
        len += len1;
        free = buf.length - len;
        buf[len] = '\0';
    }
    void sdsstrcat(String t){
       if(t.length() == 0)
           System.out.println("空字符数组");
       sdsstrcat(t.toCharArray(),t.length());
    }
    void sdscatsds(sds aa){
        sdsstrcat(aa.buf, aa.len);
    }
}
